package com.wisdom.tools;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

import android.content.Context;
import android.os.Environment;

import com.bluecall.sipua.R;

public final class CCPConfig {
	
	public static final String TAG = "CCPConfig";

	public static boolean check() {
		// if(!PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_USERNAME,
		// "").equals("")){
		// return true;
		// }

		System.out.println("UserId--->" + UserId);
		if (UserId != null && !UserId.equals("") && !UserId.equals("0")) {
			return true;
		}

		return false;
	}

	// public static String Mobile_BasicUrl="http://192.168.1.108";
	public static String Mobile_BasicUrl = "http://www.1000call.com";
	public static String Sip_Server = "www.1000call.com";
	public static String Domain_Server = "www.1000call.com";
	/**
	 * Demo rest server address info
	 */
	public static String REST_SERVER_ADDRESS = "app.cloopen.com";

	/**
	 * Demo rest server port info
	 */
	public static String REST_SERVER_PORT = "8883";

	public static String Main_Account = "";
	public static String Main_Token = "";
	public static String Sub_Account = "";
	public static String Sub_Token = "";
	public static String Sub_Name = "";
	public static String VoIP_ID = "";
	public static String VoIP_PWD = "";
	public static String App_ID = "";
	public static String Src_phone = "";
	public static String AccessToken = "";
	public static String UserId = "";
	public static String Balance = "";
	public static String CallSetting = "";
	public static String Password = "";

	public static String LOCAL_PATH = getExternalStorePath()
			+ "/config1000callV10.properties";
	private static Properties properties;

	public static String GetUserName() {
		return CCPConfig.Src_phone;
		// PreferenceManager.getDefaultSharedPreferences(Receiver.mContext).getString(Settings.PREF_USERNAME,
		// "");
	}

	/**
	 * 初始化属性
	 */
	public static boolean initProperties(Context applicationContext) {
		System.out.println(TAG + "--------initProperties");
		if (properties == null) {
			properties = new Properties();
		}
		if (isExistExternalStore()) {
			String content = readContentByFile(LOCAL_PATH);
			if (content != null) {
				try {
					properties.load(new FileInputStream(LOCAL_PATH));
					return loadConfigByProperties();
				} catch (IOException e) {
					e.printStackTrace();
					return false;
				}
			}
		}

		// if SDcar
		try {
			properties.load(applicationContext.getResources().openRawResource(
					R.raw.config));
			return loadConfigByProperties();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}

	private static void saveConfig(Properties properties) {
		try {
			FileOutputStream s = new FileOutputStream(LOCAL_PATH, false);
			properties.store(s, "");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * load config info from config.properties
	 */
	private static boolean loadConfigByProperties() {
		REST_SERVER_ADDRESS = properties.getProperty("server_address");
		REST_SERVER_PORT = properties.getProperty("server_port");

		Main_Account = properties.getProperty("main_account");
		Main_Token = properties.getProperty("main_token");
		Sub_Account/* Sub_Account */= properties.getProperty("sub_account");
		Sub_Token/* Sub_Token */= properties.getProperty("sub_token");
		VoIP_ID/* VoIP_ID */= properties.getProperty("voip_account");
		VoIP_PWD/* VoIP_PWD */= properties.getProperty("voip_password");
		App_ID = properties.getProperty("app_id");
		AccessToken = properties.getProperty("accesstoken");
		UserId = properties.getProperty("user_id");
		System.out.println("CCOConfig---------->" + UserId );
		Src_phone = properties.getProperty("src_phone");
		Balance = properties.getProperty("balance");
		CallSetting = properties.getProperty("callsetting");
		Password = properties.getProperty("password");
		return check();
	}

	public static Properties getConfigProperties() {
		return properties;
	}

	public static void setConfigProperties(String key, String value) {
		properties.setProperty(key, value);
		saveConfig(properties);
	}

	public static String readContentByFile(String path) {
		BufferedReader reader = null;
		String line = null;
		try {
			File file = new File(path);
			if (file.exists()) {
				StringBuilder sb = new StringBuilder();
				reader = new BufferedReader(new FileReader(file));
				while ((line = reader.readLine()) != null) {
					sb.append(line.trim());
				}
				return sb.toString().trim();
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null) {
					reader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	/**
	 * /sdcard
	 * 
	 * @return
	 */
	public static String getExternalStorePath() {
		if (isExistExternalStore()) {
			return Environment.getExternalStorageDirectory().getAbsolutePath();
		}
		return null;
	}

	public static boolean isExistExternalStore() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}
}
